package br.com.lambdacore.ui.stripes.actionresolver;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.stripes.controller.NameBasedActionResolver;

import org.apache.commons.lang.StringUtils;

public class BaseActionResolver extends NameBasedActionResolver {

	@Override
	protected String getBindingSuffix() {
		return "";
	}

	@Override
	protected List<String> getFindViewAttempts(String urlBinding) {
		List<String> defaultViews = super.getFindViewAttempts(urlBinding);
		List<String> customViews = new ArrayList<String>(defaultViews.size());
		for (String view : defaultViews) {
			customViews.add("/WEB-INF/pages" + view);
		}
		return customViews;
	}

	@Override
	protected String getUrlBinding(String actionBeanName) {
		String urlBinding = super.getUrlBinding(actionBeanName);
		urlBinding = this.convertToLowerCaseWithUnderscores(urlBinding);
		return "/action" + urlBinding + "/";
	}

	private String convertToLowerCaseWithUnderscores(String urlBinding) {
		String[] pathSplited = urlBinding.split("/");
		for (int i = 0; i < pathSplited.length; i++) {
			pathSplited[i] = StringUtils.uncapitalize(pathSplited[i]);

		}
		return StringUtils.join(pathSplited, "/");
	}

}
